<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 * 
 * -- MLEPP Plugin --
 * @name CopperDonations
 * @date 13-01-2011
 * @version r842
 * @website mlepp.trackmania.nl
 * @package MLEPP
 * 
 * @author Max "TheM" Klaversma <maxklaversma@gmail.com>
 *         Petri "reaby" Järvisalo <petri.jarvisalo@mbnet.fi>
 * @copyright 2010 - 2011
 * 
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\DonateCoppers;

use ManiaLive\Utilities\Console;
use ManiaLive\Utilities\String;
use ManiaLive\Data\Storage;
use ManiaLive\DedicatedApi\Connection as MlConnection;

use ManiaLive\Database\Connection;
use ManiaLivePlugins\MLEPP\DonateCoppers\Gui\DonatePanel;
use ManiaLive\Event\Dispatcher;
use ManiaLivePlugins\MLEPP\DonateCoppers\Events\onCopperDonate;

use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLivePlugins\MLEPP\Core\Mlepp;

class DonateCoppers extends \ManiaLive\PluginHandler\Plugin {    
	public static $billId = array();
	public static $toLogin = "";
	public static $donateAmountForGlobalMsg = 500;
	private $enabled = false;
	private $mlepp;
        private $show;
	private $descDonate = "add description here.";
        private $help  = "Help for plugin.";
    /**
     * onInit()
     * Function called on initialisation of ManiaLive.
     * 
     * @return void
     */
    
	function onInit() {        
		$version = 842;
		$this->setVersion($version);
        $this->setPublicMethod('getVersion');
        //ML Repository
        if(Core::RepositoryId != null){
            $this->setRepositoryId(Core::RepositoryId);
            $this->setRepositoryVersion(Core::RepositoryVersion);
        }
        
		$sinfo = $this->connection->getDetailedPlayerInfo($this->storage->serverLogin);
		if($sinfo->onlineRights == 3) $this->enabled = true;
		
		//Oliverde8 Menu
		if($this->isPluginLoaded('oliverde8\HudMenu')) {
			Dispatcher::register(\ManiaLivePlugins\oliverde8\HudMenu\onOliverde8HudMenuReady::getClass(), $this);
		}
		
	}
    
    /**
     * onLoad()
     * Function called on loading of ManiaLive.
     * 
     * @return void
     */
    
	function onLoad() {             
		Console::println('['.date('H:i:s').'] [MLEPP] Plugin: DonateCoppers r'.$this->getVersion().' by TheM & reaby.');
		$this->enableDedicatedEvents();
		$this->mlepp = Mlepp::getInstance();
		foreach($this->storage->players as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login] = DonatePanel::Create($login);
					$this->show[$login]->show();
				}
			}
		}	
		
		foreach($this->storage->spectators as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login] = DonatePanel::Create($login);
					$this->show[$login]->show();
				}
			}
		}
                $cmd = $this->registerChatCommand("donate", "donate", 1, true);
		$cmd->help = $this->descDonate;
                $cmd = $this->registerChatCommand("donate", "donate", 0, true);
		$cmd->help = $this->descDonate;
        }
	/**
	 * onOliverde8HudMenuReady()
     * Function used for adding buttons to Olivers Hud Menu.
	 * 
	 * @param mixed $menu
	 * @return void
	 */

	public function onOliverde8HudMenuReady($menu) {
		$parent = $menu->findButton(array("Menu", "Donate Coppers"));

		if(!$parent) {
			$button["style"] = "Icons128x128_1";
			$button["substyle"] = "Coppers";

			$parent = $menu->addButton("Menu", "Donate Coppers", $button);
		}

		$button["plugin"] = $this;
		$button["function"] = "Donate";

		$donations = array("100","200","500","1000","2000");
		foreach($donations as $value) {
			$button["params"] = $value;
			$menu->addButton($parent, "Donate ".$value, $button);
		}
	}

   	function onUnload() {
			parent::onUnload();
	}

    /**
     * onEndRace()
     * Function called on the end of the race.
     * 
     * @param mixed $rankings
     * @param mixed $challenge
     * @return void
     */
    
    function onEndRace($rankings, $challenge) {
		foreach($this->storage->players as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login]->hide();
				}
			}
		}	
		
		foreach($this->storage->spectators as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login]->hide();
				}
			}
		}
    }
    
    /**
     * onBeginRace()
     * Function called on begin of the race.
     * 
     * @param mixed $challengeInfo
     * @return void
     */
    
    function onBeginRace($challenge) {
		foreach($this->storage->players as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login]->show();
				}
			}
		}	
		
		foreach($this->storage->spectators as $login => $player) {
			if($this->enabled) {
				if($player->onlineRights == 3) {
					$this->show[$login]->show();
				}
			}
		}
    }

	/**
	 * onBillUpdated()
     * Function called when a bill is updated.
	 * 
	 * @param mixed $billId
	 * @param mixed $state
	 * @param mixed $stateName
	 * @param mixed $transactionId
	 * @return
	 */
     
	function onBillUpdated($billId, $state, $stateName, $transactionId) {
		if(count(self::$billId) == 0) return;
		
		foreach(self::$billId as $data) {
			if($billId == $data[0]) {
				if($state == 4) {  // Success
					$login = $data[1];
					$amount = $data[2];
					$fromPlayer = $this->storage->getPlayerObject($login);
					if ($amount < self::$donateAmountForGlobalMsg) {
						$this->mlepp->sendChat('%donate%You donated %variable%'.$amount.'%donate% coppers to the server.$z$s%donate%, Thank You.', $login);	
					}
					else {
						$this->mlepp->sendChat('%donate%The server recieved a donation of %variable%'.$amount.'%donate% coppers from %variable%'.$fromPlayer->nickName.'$z$s%donate%, Thank You.');	
					}
					Dispatcher::dispatch(new onCopperDonate($login,$amount,"MLEPP\DonateCoppers","player donate"));
					unset(self::$billId[$data[0]]);
					break;
				}
				
				if($state == 5) { // No go
					$login = $data[1];
					$amount = $data[2];
					$fromPlayer = $this->storage->getPlayerObject($login);
					$this->mlepp->sendChat('%error%No coppers billed.',$fromPlayer);	

					unset(self::$billId[$data[0]]);
					break;
				}
				
				if($state == 6) {  // Error
					$login = $data[1];
					$fromPlayer = $this->storage->getPlayerObject($login);
					$this->mlepp->sendChat('%error% There was error with player%variable%'.$fromPlayer->nickName.'$z$s%error% donation.');	
					unset(self::$billId[$data[0]]);
					break;				
				}		
			}
		}
	}
    
	/**
	 * onPlayerConnect()
     * Function called when a player connects.
	 * 
	 * @param mixed $login
	 * @param mixed $isSpectator
	 * @return void
	 */
	
	function onPlayerConnect($login,$isSpec) {
		if($this->enabled) {
			$player = $this->storage->getPlayerObject($login);
			if($player->onlineRights == 3) {
				$this->show[$login] = DonatePanel::Create($login);
				$this->show[$login]->show();
			}
		}
	}
    
        function onPlayerDisconnect($login) {
            DonatePanel::Erase($login);
            unset($this->show[$login]);         
        }
        
	/**
	 * Donate()
     * Function provides the /donate command.
	 * 
	 * @param mixed $login
	 * @param mixed $amount
	 * @return void
	 */
     
	function donate($login, $amount = null) {
            $player = $this->storage->getPlayerObject($login);
            if($player->onlineRights != 3) {
                $this->mlepp->sendChat('%error%You need to have Trackmania United in order to use this command.', $login);
                return;
            }
                if ($amount == "help" || $amount == null) {
                    $this->showHelp($login);
                    return;
                }
                if (is_numeric($amount) ) {
			$amount = (int)$amount;
		}
		else {
                    $this->mlepp->sendChat('%error%Donate takes one argument and it needs to be numeric.', $login);
		   return;
		}
                
                $fromPlayer = $this->storage->getPlayerObject($login);
		$storage = Storage::getInstance();
		$connection = MlConnection::getInstance();
		$toPlayer = new \ManiaLive\DedicatedApi\Structures\Player();
		if (empty(self::$toLogin)) {
                    $toPlayer->login = $storage->serverLogin;
		}
		else {
                    $toPlayer->login = self::$toLogin;
		}
		$fromPlayer = $storage->getPlayerObject($login);
		$billId = $connection->sendBill($fromPlayer, (int)$amount, 'Copper Donation', $toPlayer);
		DonateCoppers::$billId[$billId] = array ($billId,$login,$amount);
	}
        
        function showHelp($login) {
		$this->callPublicMethod('MLEPP\Core', 'showHelp', $login, "help for plugin ".$this->getName(), $this->help);
	}
}
?>